package org.softmed.rest.admin.mediaType;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.softmed.rest.admin.handler.HandlerChooser;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.interfaces.Recompiler;
import org.softmed.rest.admin.interfaces.Validator;
import org.softmed.rest.admin.module.ValidationException;
import org.softmed.rest.admin.interfaces.RemoveListener;
import org.softmed.swing.IconManager;

public class MediaTypeSimpleEditor extends JPanel implements ActionListener{

	HandlerChooser handlerChooser = new HandlerChooser();

	MediaTypeChooser mediaTypeChooser = new MediaTypeChooser();
	private JButton remove;
	RemoveListener removeListener;
	Validator validator;
	Recompiler recompiler;

	public MediaTypeSimpleEditor() {
//		setBackground(Color.BLUE);

		// setBorder(BorderFactory.createLineBorder(Color.black));
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 25));
		setMaximumSize(new Dimension(540, 25));
		setMinimumSize(new Dimension(540, 25));

		add(mediaTypeChooser);
		add(handlerChooser);

		remove = new JButton(IconManager.remove);
		remove.setPreferredSize(new Dimension(20, 20));
		remove.setMaximumSize(new Dimension(20, 20));
		remove.setMinimumSize(new Dimension(20, 20));
		add(remove);

		remove.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		if (source == remove) {
			removeListener.remove(this);
		}

	}


	public MediaTypeChooser getMediaTypeChooser() {
		return mediaTypeChooser;
	}

	public Validator getValidator() {
		return validator;
	}

	public void setValidator(Validator validator) {
		this.validator = validator;
		mediaTypeChooser.setValidator(validator);
	}

	public Recompiler getRecompiler() {
		return recompiler;
	}

	public void setRecompiler(Recompiler recompiler) {
		this.recompiler = recompiler;
		mediaTypeChooser.setRecompiler(recompiler);
		handlerChooser.setRecompiler(recompiler);	
	
	}

	public HandlerChooser getHandlerChooser() {
		return handlerChooser;
	}

	public void validateData() throws ValidationException {
		handlerChooser.validateData();
		mediaTypeChooser.validateData();

	}

//	@Override
//	public boolean validate(Object object) {
//		if (validator.validate(this)) {
//			recompiler.recompile();
//			return true;
//		} else {
//			// TODO show an error
//			System.out.println("THAT MEDIA TYPE IS ALREADY CONFIGURED!!!");
//			return false;
//		}
//	}

	public RemoveListener getRemoveListener() {
		return removeListener;
	}

	public void setRemoveListener(RemoveListener removeListener) {
		this.removeListener = removeListener;
	}



}
